import React ,{ useEffect }from 'react';
import { Redirect, Route, withRouter } from 'react-router-dom';
import { connect } from 'react-redux';
import ViewHoc from '@/component/HocView/ViewHoc';
import { configwx } from '@/util/share'


const pathmap = [
  '/ningJu', '/qianyanDetail', '/ningGDetail', //'/academicDetail', '/mettingSign',
]

const PrivateRoute = (props) => {
  let { component: Cmp, user: { token, role }, path, ...rest } = props;

  let path1 = props.location.search;
  let WarpCmp = ViewHoc(Cmp, props);



  useEffect(()=>{
    configwx(
      '豪森药业', '做优民族医药，做强中国创造'
    )
  })


  if (!token) return <Redirect to={{ pathname: '/login', state: path, search: path1 }} />
  
  if (role == '3') {
    if (pathmap.some(item => item == path)) {
      return <Route
        {...rest}
        render={() => <WarpCmp />}
      />
    } else {
      return <Redirect to={{ pathname: '/ningJu' }} />
    }
  }


  return <Route
    {...rest}
    render={() => <WarpCmp />}
  />

  // return (
  //   token ?
  //     (
  //       // 如果是3 并且 路径不在pathmap中 那么跳404
  //       role == '3' && pathmap.every(item => item !== path) ?
  //         <Redirect to={{ pathname: '/404' }} />
  //         :
  //         <Route
  //           {...rest}
  //           render={() => <WarpCmp />}
  //         />
  //     )

  //     :

  // )
}

const mapStateToProps = state => {
  return {
    user: state.user
  }
}



export default connect(mapStateToProps, null)(withRouter(PrivateRoute))